﻿#region Copyright(C) 2010-2010 NCS Pte. Ltd. All rights reserved.
// ==================================================================================================
// Copyright(C) 2010 NCS Pte. Ltd. All rights reserved.
//
// SYSTEM NAME	    : iConnect .Net Application Framework
// COMPONENT ID	    : NCS.IConnect.Core.Wcf
// COMPONENT DESC   : Core components for WCF.	
//
// CREATED DATE/BY  : 18 Aug 2010 / Gao Ye
//
// REVISION HISTORY :	
// DATE/BY			SR#/CS/PM#/OTHERS		DESCRIPTION OF CHANGE
// ==================================================================================================
#endregion

using System.ServiceModel.Channels;
using NCS.IConnect.Core.Utilities;

namespace NCS.IConnect.Core.Wcf.Compression
{
    /// <summary>
    /// The <see cref="CompressedMessageEncoder"/> specific <see cref="MessageEncoderFactory"/>.
    /// </summary>
    public class CompresssedMessageEncoderFactory : MessageEncoderFactory
    {
        private MessageEncoder encoder;

        /// <summary>
        /// Gets the inner <see cref="MessageEncoderFactory"/>
        /// </summary>
        public MessageEncoderFactory InnerMessageEncoderFactory{get; private set;}

        /// <summary>
        /// Gets the compressor.
        /// </summary>
        public Compressor Compressor { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="CompresssedMessageEncoderFactory"/> class.
        /// </summary>
        /// <param name="innerMessageEncoderFactory">The inner message encoder factory.</param>
        /// <param name="compressor">The compressor.</param>
        public CompresssedMessageEncoderFactory(MessageEncoderFactory innerMessageEncoderFactory, Compressor compressor)
        {
            Guard.ArgumentNotNull(innerMessageEncoderFactory, "innerMessageEncoderFactory");
            Guard.ArgumentNotNull(compressor, "compressor");
            this.Compressor = compressor;
            this.InnerMessageEncoderFactory = innerMessageEncoderFactory;
            encoder = new CompressedMessageEncoder(this.InnerMessageEncoderFactory.Encoder,compressor);
        }

        /// <summary>
        /// When overridden in a derived class, gets the message encoder that is produced by the factory.
        /// </summary>
        /// <returns>The <see cref="T:System.ServiceModel.Channels.MessageEncoder"/> used by the factory.</returns>
        public override MessageEncoder Encoder
        {
            get { return encoder; }
        }

        /// <summary>
        /// When overridden in a derived class, gets the message version that is used by the encoders produced by the factory to encode messages.
        /// </summary>
        /// <returns>The <see cref="T:System.ServiceModel.Channels.MessageVersion"/> used by the factory.</returns>
        public override MessageVersion MessageVersion
        {
            get { return encoder.MessageVersion; }
        }

        /// <summary>
        /// Returns a message encoder that can be used to correlate messages in session-based exchanges.
        /// </summary>
        /// <returns>
        /// The <see cref="T:System.ServiceModel.Channels.MessageEncoder"/> that is used in session-based exchanges of messages.
        /// </returns>
        public override MessageEncoder CreateSessionEncoder()
        {
            return new CompressedMessageEncoder(this.InnerMessageEncoderFactory.CreateSessionEncoder(),this.Compressor);
        }
    }
}
